package ysw.jdbc;

import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import ysw.dao.ObdDao;
import ysw.databaseconnection.DBConn;

import ysw.vo.Obd;

public class ObdJdbc implements ObdDao{
private  Connection conn=null;
    private PreparedStatement psmt=null;
    private ResultSet rs=null;
    public ObdJdbc(){}
    public Connection getConn(){
        try{
            if(this.conn==null||this.conn.isClosed()){
                DBConn mc=new DBConn();
                this.conn=mc.getConn();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return conn;
    }

    public Obd addObd(Obd obd) {
     String sql="insert into obds values(?,?,?)";
        try{
            psmt=this.getConn().prepareStatement(sql);
    
            psmt.setInt(1,obd.getObdid());   
            psmt.setString(2,obd.getObdtype());
            psmt.setString(3,obd.getObddeal());
     
            psmt.execute();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(psmt!=null){
                    psmt.close();
                    psmt=null;
                }
                if(conn!=null){
                    conn.close();
                    conn=null;
                }
            }catch(SQLException e){
                        e.printStackTrace();
                        }
            }
            return obd;
        }  
    

 
    public List showObd() throws SQLException {
      String sql="select * from obds";
        List obdList=new ArrayList();
    try{
        psmt=this.getConn().prepareStatement(sql);
        rs=psmt.executeQuery();
        while(rs.next()){
      Obd obd=new Obd();
            obd.setObdid(rs.getInt("obdid"));
            obd.setObdtype(rs.getString("obdtype"));
            obd.setObddeal(rs.getString("obddeal"));

  
        }
        return obdList;
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try{
             if(rs!=null){
                   rs.close();
                   rs=null;
                }
                if(psmt!=null){
                    psmt.close();
                    psmt=null;
                }
                if(conn!=null){
                    conn.close();
                    conn=null;
                }
            }catch(SQLException e){
                        e.printStackTrace();
                        }
            }
            return obdList;
} 

}
